<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="app-nav-bar">
      <van-button
        slot="title"
        class="search-btn"
        icon="search"
        type="info"
        round
        size="small"
        to="/search"
        >搜索</van-button
      >
    </van-nav-bar>

    <!-- 文章频道列表 -->
    <van-tabs v-model="active" class="channel-tabs">
      <van-tab
        class="tab-item"
        v-for="item in channels"
        :title="item.name"
        :key="item.id"
      >
        <ArticleList :channel="item"></ArticleList>
      </van-tab>
      <div slot="nav-right" style="width: 33px; flex-shrink: 0"></div>
      <div
        class="wap-nav-wrap"
        slot="nav-right"
        @click="isChannelEditShow = true"
      >
        <van-icon name="wap-nav"></van-icon>
      </div>
    </van-tabs>

    <!-- 弹出层 -->
    <van-popup
      v-model="isChannelEditShow"
      get-container="body"
      position="bottom"
      closeable
      close-icon-position="top-left"
      :style="{ height: '100%' }"
      class="channel-edit-popup"
    >
      <ChannelEdit
        :users-channels="channels"
        @update-active="active = $event"
      ></ChannelEdit>
    </van-popup>
  </div>
</template>
<script>
import { getUserChannels } from "@/api/login";
import { mapState } from "vuex";
import ArticleList from "./components/articleList";
import ChannelEdit from "./components/channel-edit";
export default {
  name: "Home",
  data() {
    return {
      active: 0,
      channels: [], //频道列表
      isChannelEditShow: false,
    };
  },
  components: {
    ArticleList,
    ChannelEdit,
  },
  computed: {
    ...mapState(["user"]),
  },
  created() {
    this.getUserChannels();
  },
  methods: {
    async getUserChannels() {
      let channels = [];
      if (this.user) {
        const { data } = await getUserChannels();
        channels = data.data.channels;
      }
      // 把处理好的数据放到data中
      this.channels = channels;
    },
    onUpdateActive(index) {
      console.log(index);
      this.active = index;
    },
  },
};
</script>
<style lang="less" scoped>
.home-container {
  /deep/ .van-nav-bar__content {
    background: #1989fa;
  }
  /deep/ .van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 277px;
    height: 32px;
    border: none;
    background: #7cbbfa;
    .van-icon {
      font-size: 16px;
      color: #fff !important;
    }
    .van-buttom__text {
      font-size: 14px;
    }
  }

  .channel-tabs {
    /deep/ .van-tab {
      // border-right: 1px solid #edeff3;
      border-bottom: 1px solid #edeff3;
    }
    /deep/ .van-tabs__line {
      bottom: 20px;
      width: 20px !important;
      height: 2px;
      background-color: #3296fa;
    }
  }

  .wap-nav-wrap {
    position: fixed;
    right: 0;
    width: 33px;
    height: 44px;
    line-height: 44px;
    background-color: #fff;
    text-align: center;
    opacity: 0.9;
    .van-icon {
      font-size: 20px;
    }
  }
}
</style>
